package com.gxuwz.attendance.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import com.gxuwz.attendance.entity.Student;
import com.gxuwz.attendance.service.ClassService;
import com.gxuwz.attendance.service.StudentService;
import com.gxuwz.attendance.util.JsonUtil;
import com.gxuwz.attendance.vo.Page;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class StudentAction extends BaseAction implements ModelDriven<Student>,Preparable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static String STUDENT_INDEX = "/page/student/student_list.jsp";
	private Student student;
	@Autowired
	private StudentService service;
	@Autowired
	private ClassService classService;
	private String rows;
	private String page;
	private String ids;
	private Map<String, Object> map = new HashMap<String, Object>();
	
	/**
	 * 批量删除学生信息
	 */
	public void delete() {
		boolean flag = true;
		try {
			flag = service.delete(ids);
		}catch (Exception e) {
			map.put("type", "error");
			map.put("msg", "删除失败");
			e.printStackTrace();
		}
		map.put("type", "success");
		map.put("msg", "删除成功");
		if(flag == false) {
			map.put("type", "error");
			map.put("msg", "删除失败");
		}
		
		JsonUtil.toJons(ServletActionContext.getResponse(), map);
	}
	
	/**
	 * 添加学生信息
	 */
	public void add() {
		boolean flag = service.add(student);
		
		System.out.println("flag:" + flag);
		
		if(flag != true) {
			map.put("type", "error");
			map.put("msg", "添加失败");
		}else {
			map.put("type", "success");
			map.put("msg", "添加成功");
		}
		
		JsonUtil.toJons(ServletActionContext.getResponse(), map);
	}
	
	
	/**
	 * 修改学生信息
	 */
	public void update() {
		boolean flag = service.update(student);
		if(flag == false) {
			map.put("type", "error");
			map.put("msg", "修改失败");
		}else {
			map.put("type", "success");
			map.put("msg", "修改成功");
		}
		
		JsonUtil.toJons(ServletActionContext.getResponse(), map);
		
	}
	
	
	/**
	 * 跳转到学生首页
	 * @return
	 */
	public String index() {
		classService.findByName(null);
		forwardView = STUDENT_INDEX;
		
		return SUCCESS;
	}
	/**
	 * 模糊查询(学生姓名)
	 */
	public void list() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		map.put("rows", service.findByPage(student, new Page(Integer.parseInt(page), Integer.parseInt(rows))));
		map.put("total", session.getAttribute("studentSize"));
		
		JsonUtil.toJons(ServletActionContext.getResponse(), map);
	}

	
	@Override
	public void prepare() throws Exception {
		if(student == null) {
			student = new Student();
		}
	}

	@Override
	public Student getModel() {
		return student;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}
	
	
}
